package com.jwgf.smarowindingmachine_unity.ui;

import java.lang.reflect.Field;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jwgf.adapter.CommonAdapter;
import com.jwgf.adapter.ViewHolder;
import com.jwgf.common.DataAccessRule;
import com.jwgf.group.GroupMap;
import com.jwgf.objectbox.ObjectBox;
import com.jwgf.objectbox.SpindleParameter;
import com.jwgf.smarowindingmachine_unity.R;
import com.jwgf.smarowindingmachine_unity.logic.SmaroWindingMachineCanReadSucceedListener;
import com.jwgf.smarowindingmachine_unity.logic.SmaroWindingMachineDataAccessRules;
import com.jwgf.smarowindingmachine_unity.logic.UnitUtils;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import androidx.fragment.app.DialogFragment;

public class QueryWorkingDataDialog extends DialogFragment {
    public static final String SPINDLE_INDEX = "index";
    private static final String NAME = "name";
    private static final String VALUE = "value";
    private int spindle_index = 1;
    private Runnable callback;
    private ListView listView;
    private AlertDialog dlg;
    private CommonAdapter adapter;
    private List<Map<String, String>> list = new ArrayList<Map<String, String>>();
    private SmaroWindingMachineCanReadSucceedListener swm = SmaroWindingMachineCanReadSucceedListener.getInstance();
    private SmaroWindingMachineDataAccessRules rules = SmaroWindingMachineDataAccessRules.getInstance();
    private SpindleParameter para;

    public void setCallBack(Runnable callback) {
        this.callback = callback;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        spindle_index = getArguments().getInt(SPINDLE_INDEX);
        para = ObjectBox.get().boxFor(SpindleParameter.class)
                .get(GroupMap.getInstance().getSpindleParameterIdFromSpindle(spindle_index));
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View v = inflater.inflate(R.layout.list_view, null);
        listView = (ListView) v.findViewById(R.id.list);
        listView.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
        listView.setAdapter(adapter = new CommonAdapter<Map<String, String>>(getActivity(), getData(),
                R.layout.item_para_detail_list) {
            @Override
            public void convert(final ViewHolder holder, final Map<String, String> item) {
                holder.setText(R.id.para_text, item.get(NAME));
                holder.setText(R.id.para_value, item.get(VALUE));
            }
        });
        builder.setTitle("工作参数");
        builder.setView(v).setPositiveButton("确定", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                show(false);
            }
        }).setNegativeButton("取消", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                show(false);
            }
        });
        dlg = builder.create();
        return dlg;
    }

    private List<Map<String, String>> getData() {
        HashMap<String, String> item = null;

        for (String key : SmaroWindingMachineDataAccessRules.getInstance().getKeySet()) {
            if (key.equals(String.valueOf(0x10 << 8 | 0)) || key.equals(String.valueOf(0x10 << 8 | 1))
                    || key.equals(String.valueOf(0x20 << 8 | 0)) || key.equals(String.valueOf(0x20 << 8 | 1))
                    || key.equals(String.valueOf(0x20 << 8 | 2)) || key.equals(String.valueOf(0x20 << 8 | 3))
                    || key.equals(String.valueOf(0x21 << 8 | 0))
                    || key.equals(String.valueOf(0x21 << 8 | 1)) || key.equals(String.valueOf(0x21 << 8 | 2))
                    || key.equals(String.valueOf(0x21 << 8 | 3)) || key.equals(String.valueOf(0x22 << 8 | 0))
                    || key.equals(String.valueOf(0x22 << 8 | 1)) || key.equals(String.valueOf(0x22 << 8 | 2))
                    || key.equals(String.valueOf(0x23 << 8 | 0)) || key.equals(String.valueOf(0x23 << 8 | 1))
                    || key.equals(String.valueOf(0x23 << 8 | 2)) || key.equals(String.valueOf(0x23 << 8 | 3))
                    || key.equals(String.valueOf(0x24 << 8 | 0)) || key.equals(String.valueOf(0x24 << 8 | 1))
                    || key.equals(String.valueOf(0x24 << 8 | 2)) || key.equals(String.valueOf(0x25 << 8 | 0))
                    || key.equals(String.valueOf(0x25 << 8 | 1)) || key.equals(String.valueOf(0x25 << 8 | 2))
                    || key.equals(String.valueOf(0x26 << 8 | 0)) || key.equals(String.valueOf(0x26 << 8 | 1))
                    || key.equals(String.valueOf(0x26 << 8 | 2)) || key.equals(String.valueOf(0x27 << 8 | 0))
                    || key.equals(String.valueOf(0x27 << 8 | 1)) || key.equals(String.valueOf(0x27 << 8 | 2))
                    || key.equals(String.valueOf(0x27 << 8 | 3)) || key.equals(String.valueOf(0x27 << 8 | 4))
                    || key.equals(String.valueOf(0x27 << 8 | 5)) || key.equals(String.valueOf(0x28 << 8 | 0))
                    || key.equals(String.valueOf(0x28 << 8 | 1)) || key.equals(String.valueOf(0x28 << 8 | 2))
                    || key.equals(String.valueOf(0x29 << 8 | 0)) || key.equals(String.valueOf(0x29 << 8 | 1))
                    || key.equals(String.valueOf(0x29 << 8 | 2)) || key.equals(String.valueOf(0x30 << 8 | 0))
                    || key.equals(String.valueOf(0x30 << 8 | 1)) || key.equals(String.valueOf(0x30 << 8 | 2))
                    || key.equals(String.valueOf(0x31 << 8 | 0)) || key.equals(String.valueOf(0x31 << 8 | 2))
                    || key.equals(String.valueOf(0x32 << 8 | 0)) || key.equals(String.valueOf(0x32 << 8 | 1))
                    || key.equals(String.valueOf(0x32 << 8 | 2)) || key.equals(String.valueOf(0x33 << 8 | 0))
                    || key.equals(String.valueOf(0x33 << 8 | 1)) || key.equals(String.valueOf(0x33 << 8 | 2))
                    || key.equals(String.valueOf(0x2C << 8 | 0)) || key.equals(String.valueOf(0x2C << 8 | 1))
                    || key.equals(String.valueOf(0x2C << 8 | 2))) {
                item = new HashMap<String, String>();
                DataAccessRule dar = rules.getDataAccessRule(key);
                if (dar != null) {
                    if (key.equals(String.valueOf(0x30 << 8 | 0)) || key.equals(String.valueOf(0x30 << 8 | 1))
                            || key.equals(String.valueOf(0x30 << 8 | 2)) || key.equals(String.valueOf(0x32 << 8 | 0))
                            || key.equals(String.valueOf(0x31 << 8 | 0)) || key.equals(String.valueOf(0x31 << 8 | 2))
                            || key.equals(String.valueOf(0x32 << 8 | 1)) || key.equals(String.valueOf(0x32 << 8 | 2))
                            || key.equals(String.valueOf(0x33 << 8 | 2))) {
                        String a = Integer.toHexString(Integer.valueOf(swm.getQueryData(Integer.valueOf(key))));
                        item.put(VALUE, String.valueOf(a));
                    } else if (key.equals(String.valueOf(0x33 << 8 | 0)) || key.equals(String.valueOf(0x33 << 8 | 1))) {
                        DecimalFormat df = new DecimalFormat("0.00");
                        float query_data = Float.valueOf(swm.getQueryData(Integer.valueOf(key))) / 100f;
                        item.put(VALUE, df.format(query_data));
                    } else if (key.equals(String.valueOf(0x29 << 8 | 0)) || key.equals(String.valueOf(0x29 << 8 | 1))
                            || key.equals(String.valueOf(0x24 << 8 | 0))) {
                        int len = Math.round(
                                UnitUtils.PULSE_TO_LENGTH_FACTOR * Integer.valueOf(swm.getQueryData(Integer.valueOf(key)))
                                        / Float.parseFloat(para.slipCoefficient));
                        item.put(VALUE, String.valueOf(len));
                    } else if (key.equals(String.valueOf(0x29 << 8 | 2))) {
                        DecimalFormat df = new DecimalFormat("0.00");
                        float query_data = Float.valueOf(swm.getQueryData(Integer.valueOf(key))) / 1024f;
                        item.put(VALUE, df.format(query_data));
                    } else if (key.equals(String.valueOf(0x20 << 8 | 2))) {
                        DecimalFormat df = new DecimalFormat("0.0");
                        int query_data = Integer.valueOf(swm.getQueryData(Integer.valueOf(key)));
                        float helixUI = (float) 0.0;
                        if (query_data == 0) {
                            helixUI = (float) 2.0;
                            item.put(VALUE, df.format(helixUI));
                        } else if (query_data == 1) {
                            helixUI = (float) 2.5;
                            item.put(VALUE, df.format(helixUI));
                        }
                    } else if (key.equals(String.valueOf(0x20 << 8 | 3))) {
                        int query_data = Integer.valueOf(swm.getQueryData(Integer.valueOf(key)));
                        final String[] content = getResources().getStringArray(R.array.tension_pad_type);
                        item.put(VALUE, content[query_data]);
                    } else {
                        item.put(VALUE, swm.getQueryData(Integer.valueOf(key)));
                    }
                    item.put(NAME, rules.getName(key));
                }
                list.add(item);
            }
        }

        return list;
    }

    void show(boolean isShow) {
        try {
            Field field = dlg.getClass().getSuperclass().getDeclaredField("mShowing");
            field.setAccessible(true);
            field.set(dlg, !isShow);
            dlg.dismiss();

        } catch (Exception e) {

        }
    }

}
